<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>相加等于数字n的完全平方数的最少数量</title>
</head>
<body>
  <script>
    function fn(n) {
      let num = [0]
      // 最多循环12次
      for (let i = 1; i <= n; i++) {
        // 将循环的添加到 num 中
        num[i] = i
        // console.log(num, num[i])
        for (let j = 1; j * j <= i; j++) {
          num[i] = Math.min(num[i], num[i - j * j] + 1)
        }
      }
      return num[n]
    }
    console.log(fn(12));
  </script>
</body>
</html>